/**
 * Created by Administrator on 2017/4/1 0001.
 */
angular.module('App')

    /*
    * 调用封装好的服务 在封装成指令    放大图片
    * */
    .directive('pullImgs', ['Util','dataCookies',function(Util,dataCookies) {
        return {
                restrict : 'EA',  // element  attr
                link : function(scope, element, attrs) {
                            //console.log(attrs);
                    $(element).click(function(){

                        Util.bigImg( attrs.src );
                        //console.log( scope.model );
                    })
                }
            }
    }])
    /*
     * 搜索
     * */
    .directive('searchH', ['Util','dataCookies','$http',function(Util,dataCookies,$http) {
        return {
            restrict : 'EA',  // element  attr
            link : function(scope, element, attrs) {

                // element.onchange = function(){
                //     console.log( $(element) );
                //     console.log( scope.search );
                // }
                scope.$watch('search',function(news,old){
                    $http.post('search',{ kw:news }).success(function(){
                        //console.log(36);
                    }).error(function(){
                        scope.testData = 'test';
                    })
                })


            }
        }
    }])
    /*
     * 阻止冒泡
     * */
    .directive('preventDefault',function(){
        return{
            restrict:'AE',
            link:function( scope,elem,attr ){
                $(elem).click(function(){
                    event.cancelBubble=true;
                })

            }
        }
    })
    /*
     * 阻止冒泡
     * */
    .directive('getHighcharts',['Util',function(Util){
        return{
            restrict:'AE',
            link:function( scope,elem,attr ){
                $(elem).click(function(){
                    //console.log( attr.hight.split(',') );
                    var a = attr.hight.split(','),b=[];
                    angular.forEach(a,function( v,i ){
                        //console.log( v );
                        v = Number(v);
                        b.push(v);
                    })
                    //var data2 = [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4];
                    $('#highChartBox').fadeToggle(2000);
                    Util.gethighChart( $('#highChartBox'),'pie','step1',b);
                })

            }
        }
    }])
    /*
     * 阻止冒泡2
     * */
    .directive('areaCharts',['Util',function(Util){
        return{
            restrict:'AE',
            link:function( scope,elem,attr ){
                    //console.log( attr.hight.split(',') );
                    var a = attr.hight.split(','),b=[];
                    angular.forEach(a,function( v,i ){
                        console.log( v );
                        v = Number(v);
                        b.push(v);
                    });
                    //var data2 = [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4];
                    //$('.container').fadeToggle(2000);
                Util.areaHighcharts($(".container"),'天明','area',b);

            }
        }
    }])
    /*
     * 阻止冒泡3 显示的
     * */
    .directive('clickArea',['Util',function(Util){
        return{
            restrict:'AE',
            link:function( scope,elem,attr ){
                //console.log( attr.hight.split(',') );
                $(elem).click(function () {
                    $('.clickContainer').show();
                    var a = attr.hight.split(','),b=[];
                    angular.forEach(a,function( v,i ){
                        console.log( v );
                        v = Number(v);
                        b.push(v);
                    });
                    //var data2 = [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4];
                    $('.clickContainer').fadeToggle(10000);
                    Util.areaHighcharts($(".clickContainer"),'天明','area',b);
                })

            }
        }
    }])

    /*
     * 上传图片
     * */
    .directive('loadImg',[function () {
        return{
            restrict:'AE',
            link:function (scope,elem,attr) {

            }
        }
    }])
    /*
    * 点击 显示 隐藏 发
    * */
    .directive('showBox',function(){
        return{
            restrict:'AE',
            link:function( scope,elem,attr ){
                $(elem).click(function(){
                    $(this).next().toggle();
                })
            }
        }
    })
    /*
     * community页面 点击 图片 用户 中的 x 按钮，关闭 下拉框
     * */
    .directive('closeBox',function(){
        return{
            restrict:'AE',
            link:function( scope,elem,attr ){
                $(elem).click(function(){
                    $(this).parent().hide();//parent() 对父元素 的操作
                })

            }
        }
    })
    /*
     * 社区页面  file 读取 图片 内容
     * */
    .directive('communityUloadimg',['$http',function($http){
        return{
            restrict:'AE',
            link:function( scope,elem,attr ){
                $(elem).change(function(  ){
                    for( var i = 0;i<this.files.length;i++ ){
                        var file = this.files[i];
                        var reader = new FileReader();
                        reader.onload = function (e) {
                            if( scope.hotWeiB.nowImgTotal>=9 ){
                                scope.hotWeiB.nowImgTotal=scope.hotWeiB.nowImgTotal;
                            }else{
                                $http.post('../json/activity.json',{
                                    img: e.target.result
                                }).success(function(ret){
                                    if( ret.ret_code==1 ){
                                        scope.hotWeiB.nowImgTotal ++;
                                        scope.hotWeiB.showImgTotal<=0?scope.hotWeiB.showImgTotal=scope.hotWeiB.showImgTotal:scope.hotWeiB.showImgTotal--;
                                        $(elem).parent().parent().before('<li class="add_imgli" style="width:80px;margin: 10px 10px 0 0;height:80px;position:relative;overflow: hidden;"><i class="i-pic-close-send"></i><img width="80" height="80" src="'+ ret.ret_msg +'"></li>');
                                    }
                                })
                            }
                            scope.$apply();
                            $(elem).val('') ;
                        }
                        reader.readAsDataURL(file);
                    }
                })
                $('#BlogFileList').on('click','.i-pic-close-send',function(){
                    $(this).parent().remove();
                    scope.hotWeiB.nowImgTotal--;
                    scope.hotWeiB.showImgTotal++;
                    scope.$apply();
                })
            }
        }
    }])


